package servlets;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.xml.internal.rngom.ast.builder.Include;

import capaLogica.Comentario;
import capaLogica.ComentarioBO;
import capaLogica.Evento;
import capaLogica.EventoBO;
import capaLogica.Multimedia;
import capaLogica.MultimediaBO;

/**
 * Servlet implementation class MultimediaCO
 */
public class MultimediaCO extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MultimediaCO() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String accion = request.getParameter("accion");
		try {
			procesarAccion(accion, request, response);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void procesarAccion(String accion, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException, ParseException {
		if (accion.equals("subirMultimediaF")) {
			subirMultimediaF(request, response);
		}else if ( accion.equals("borrarMultimedia")){
			borrarMultimedia(request,response);
		}else if ( accion.equals("subirFoto")){
			subirFoto(request,response);
		}else if ( accion.equals("subirVideo")){
			subirVideo(request,response);
		}else if ( accion.equals("subirMultimediaV")){
			subirMultimediaV(request,response);
		}
	}

	private void subirMultimediaV(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String idEvento = request.getParameter("idEvento");
		request.setAttribute("idEvento", idEvento);
		request.getRequestDispatcher("subirMultimediaVideo.jsp").include(request, response);
		
	}

	private void subirVideo(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String ruta = request.getParameter("ruta");
		String descripcion = request.getParameter("descripcion");
		String idEvento = request.getParameter("idEvento");
		
		MultimediaBO mBO = new MultimediaBO();
		Multimedia m = new Multimedia();
		m.setDescripcion(descripcion);
		m.setEsVideo("1");
		m.setRuta(ruta);
		
		Multimedia a = mBO.insertarMultimedia(m);
		mBO.asociarEvento(idEvento, a.getIdMultimedia());
		
		request.getRequestDispatcher("evento?accion=verEvento&idEvento="+idEvento).include(request, response);
		
		
	}

	private void subirMultimediaF(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String idEvento = request.getParameter("idEvento");
		request.setAttribute("idEvento", idEvento);
		request.getRequestDispatcher("subirMultimedia.jsp").include(request, response);
		
	}

	private void borrarMultimedia(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String idMultimedia = request.getParameter("idMultimedia");
		String idEvento = request.getParameter("idEvento");
		MultimediaBO mBO = new MultimediaBO();
		mBO.borrarMultimedia(idMultimedia);
	
		ComentarioBO comentarioBO = new ComentarioBO();
		List<Comentario> listaC = comentarioBO.recuperarTodos();
		
		MultimediaBO multiBO = new MultimediaBO();
		List<Multimedia> listaFotos = multiBO.recuperarFotosByEvento(idEvento);
		List<Multimedia> listaVideo = multiBO.recuperarVideoByEvento(idEvento);
		
		EventoBO eventoB = new EventoBO();
		Evento evento = eventoB.recuperarEventoById(idEvento);
		
		request.setAttribute("listaComentario", listaC);
		request.setAttribute("listaFoto", listaFotos);
		request.setAttribute("listaVideo", listaVideo);
		request.setAttribute("evento", evento);
		request.getRequestDispatcher("visualizarEvento.jsp").include(request, response);
		
	}

	private void subirFoto(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String ruta = request.getParameter("ruta");
		String descripcion = request.getParameter("descripcion");
		String idEvento = request.getParameter("idEvento");
		
		MultimediaBO mBO = new MultimediaBO();
		Multimedia m = new Multimedia();
		m.setDescripcion(descripcion);
		m.setEsVideo("0");
		m.setRuta(ruta);
		
		Multimedia a = mBO.insertarMultimedia(m);
		mBO.asociarEvento(idEvento, a.getIdMultimedia());
		
		request.getRequestDispatcher("evento?accion=verEvento&idEvento="+idEvento).include(request, response);
		
	}

}
